﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using COMExcel = Microsoft.Office.Interop.Excel;

namespace QuanLyKhachSan
{
    public partial class frmBaoCaoMatDoSuDungPhong : Form
    {
        public frmBaoCaoMatDoSuDungPhong()
        {
            InitializeComponent();
        }
        private void frmBaoCaoMatDoSuDungPhong_Load(object sender, EventArgs e)
        {
            //Lấy giờ hệ thống
            string gio = DateTime.Now.ToLongTimeString();
            string ngay = DateTime.Now.ToLongDateString();
            string chuoi = ngay + " " + gio;
            txtNgayHienTai.Text = chuoi;

            //Hiển thị thông tin lên ComboBox tháng
            int i;
            for (i = 1; i <= 12; i++)
            {
                cboThang.Items.Add(i);
            }
            cboThang.SelectedIndex = 0;
            cboThang.DropDownStyle = ComboBoxStyle.DropDownList;
        }
        //kiem tra thang hop le
        private bool KiemTraThangPhaiNhoHonThangHienTai(int thang, int nam)
        {
            int thangHienTai = DateTime.Now.Month;
            int namHienTai = DateTime.Now.Year;
            int ngayHienTai = DateTime.Now.Day;

            DateTime monthStart = new DateTime(namHienTai, thangHienTai, 1);
            DateTime monthEnd = monthStart.AddMonths(1);
            TimeSpan ts = monthEnd.Subtract(monthStart);
            int DayInMonth = ts.Days;

            if (namHienTai > nam)
            {
                return true;
            }
            if (nam > namHienTai)
                return false;
            if (thangHienTai > thang)
                return true;
            if (thang > thangHienTai)
                return false;
            if (ngayHienTai < DayInMonth)
                return false;
            return true;
        }
        // kiem tra nam phai co du 4 so
        private bool KiemTraNam(string chuoiNam)
        {
            if (chuoiNam.Length != 4)
            {
                return false;
            }
            return true;
        }
        private float TinhTyLeSoNgayThue(int thang, int nam, int soNgayThue)
        {
            float tyLe = 0;

            DateTime monthStart = new DateTime(nam, thang, 1);
            DateTime monthEnd = monthStart.AddMonths(1);
            TimeSpan ts = monthEnd.Subtract(monthStart);
            int DayInMonth = ts.Days;

            tyLe = ((float)soNgayThue / DayInMonth) * 100;
            return (float)Math.Round(tyLe,2);

        }
        private void btnXem_Click(object sender, EventArgs e)
        {
            bool kq = KiemTraNam(txtNam.Text);
            btnInFileExcel.Enabled = false;
            lblKQ.Visible = false;
            lblThangNam.Visible = false;
            if (kq == false)
            {
                MessageBox.Show("Nam Phai Co 4 Chu So! Hay Thu Lai", "Thong Bao", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                txtNam.Focus();
                dvgDanhSachPhong.Rows.Clear();
            }
            else
            {

                int thang = int.Parse(cboThang.SelectedItem.ToString());
                int nam = int.Parse(txtNam.Text);

                kq = KiemTraThangPhaiNhoHonThangHienTai(thang, int.Parse(txtNam.Text));
                // MessageBox.Show(kq.ToString());
                if (kq == false)
                {
                    MessageBox.Show("Thang Can Xem Bao Cao Phai Nho Hon Thang Hien Tai!\n\nHoac Phai La Ngay Cuoi Thang Hien Tai Moi Duoc Xem Bao Cao", "Thong Bao", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    dvgDanhSachPhong.Rows.Clear();
                    txtNam.Focus();
                }
                else
                {
                    //MessageBox.Show(TinhTyLeSoNgayThue(thang, nam, 23).ToString());
                    lblThangNam.Text = thang.ToString() + "/" + nam.ToString();

                    lblKQ.Text ="KẾT QUẢ BÁO CÁO THÁNG "+ lblThangNam.Text;
                    lblKQ.Visible = true;
                    DataTable dt = PhongBUS.LayDanhSachPhong();
                    dvgDanhSachPhong.Rows.Clear();
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            DataRow dr = dt.Rows[i];
                            int maPhongThue = int.Parse(dr[0].ToString());
                            string tenPhong = dr[1].ToString();

                            dvgDanhSachPhong.Rows.Add();
                            dvgDanhSachPhong.Rows[i].Cells["stt"].Value = (i + 1).ToString();
                            dvgDanhSachPhong.Rows[i].Cells["maPhong"].Value = maPhongThue.ToString();
                            dvgDanhSachPhong.Rows[i].Cells["phong"].Value = tenPhong;

                            int soNgay = PhongDAO.SoNgayThueCuaPhongTrongThang(maPhongThue, thang, nam);
                            float tyLeThue = TinhTyLeSoNgayThue(thang, nam, soNgay);

                            dvgDanhSachPhong.Rows[i].Cells["soNgayThue"].Value = soNgay.ToString();
                            dvgDanhSachPhong.Rows[i].Cells["tyLe"].Value = tyLeThue.ToString() + "%";

                        }
                    }

                    btnInFileExcel.Enabled = true;
                }
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnInFileExcel_Click(object sender, EventArgs e)
        {
            // Khởi động chtr Excell
            COMExcel.Application exApp = new COMExcel.Application();
            System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

            exApp.Visible = false;
            // Thêm file temp xls
            COMExcel.Workbook exBook = exApp.Workbooks.Add(COMExcel.XlWBATemplate.xlWBATWorksheet);

            // Lấy sheet 1.
            COMExcel.Worksheet exSheet = (COMExcel.Worksheet)exBook.Worksheets[1];


            COMExcel.Range r = (COMExcel.Range)exSheet.Cells[1, 2];
            COMExcel.Range r2 = (COMExcel.Range)exSheet.Cells[1, 5];

            // Ghi dữ liệu loi ngo
            r.Value2 = "BÁO CÁO MẬT ĐỘ SỬ DỤNG PHÒNG";
            r.Font.Bold = true;
            r.Font.Size = 16;
            r.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            r.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
            exApp.get_Range(r, r2).Merge(Type.Missing);
            // Giãn cột
            r.Columns.AutoFit();
            r.Rows.RowHeight = 30;

            COMExcel.Range rThang = (COMExcel.Range)exSheet.Cells[2, 2];
            COMExcel.Range rThang2 = (COMExcel.Range)exSheet.Cells[2, 5];

            // Ghi dữ liệu thang
            rThang.Value2 = "Tháng " + lblThangNam.Text;// "THÁNG " + cboThang.SelectedItem.ToString() + "/" + txtNam.Text;
            rThang.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            rThang.Font.Italic = true;
            rThang.Font.Size = 13;
            exApp.get_Range(rThang, rThang2).Merge(Type.Missing);

            //ghi du lieu ten cot
            COMExcel.Range rSTT = (COMExcel.Range)exSheet.Cells[4, 2];
            rSTT.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            rSTT.Value2 = "STT";
            rSTT.Font.Bold = true;
            rSTT.Font.Size = 12;
            rSTT.Columns.ColumnWidth = 5;

            COMExcel.Range rPhong = (COMExcel.Range)exSheet.Cells[4, 3];
            rPhong.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            rPhong.Value2 = "Phòng";
            rPhong.Font.Bold = true;
            rPhong.Font.Size = 12;
            rPhong.Columns.ColumnWidth = 20;

            COMExcel.Range rSoNgayThue = (COMExcel.Range)exSheet.Cells[4, 4];
            rSoNgayThue.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            rSoNgayThue.Value2 = "Số Ngày Thuê";
            rSoNgayThue.Font.Bold = true;
            rSoNgayThue.Font.Size = 12;
            rSoNgayThue.Columns.ColumnWidth = 15;

            COMExcel.Range rTyLe = (COMExcel.Range)exSheet.Cells[4, 5];
            rTyLe.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            rTyLe.Value2 = "Tỷ Lệ";
            rTyLe.Font.Bold = true;
            rTyLe.Font.Size = 12;
            rTyLe.Columns.ColumnWidth = 15;

            for (int i = 0; i < dvgDanhSachPhong.RowCount; i++)
            {
                COMExcel.Range rDuLieuSTT = (COMExcel.Range)exSheet.Cells[i+5, 2];
                rDuLieuSTT.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                rDuLieuSTT.Value2 = (i + 1);

                COMExcel.Range rDuLieuPhong = (COMExcel.Range)exSheet.Cells[i + 5, 3];
                rDuLieuPhong.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                rDuLieuPhong.Value2 = dvgDanhSachPhong.Rows[i].Cells["phong"].Value.ToString();

                COMExcel.Range rDuLieuSoNgay = (COMExcel.Range)exSheet.Cells[i + 5, 4];
                rDuLieuSoNgay.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                rDuLieuSoNgay.Value2 = dvgDanhSachPhong.Rows[i].Cells["soNgayThue"].Value.ToString();

                COMExcel.Range rDuLieuTyLe = (COMExcel.Range)exSheet.Cells[i + 5, 5];
                rDuLieuTyLe.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                rDuLieuTyLe.Value2 = dvgDanhSachPhong.Rows[i].Cells["tyLe"].Value.ToString();
            }
            
            System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
            exApp.Visible = true; 
            // Thuc hien xong thao tac in thi an button di
            btnInFileExcel.Enabled = false;
        }

        private void txtNam_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        
    }
}